/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ngramassigment.fileprocess;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import ngramassigment.Config;

/**
 *
 * @author Tran
 */
public class ProcessSeparate {

    private static ProcessSeparate instance = null;

    public static ProcessSeparate getInstance() {
        if (instance == null) {
            instance = new ProcessSeparate();
        }
        return instance;
    }

    public ProcessSeparate() {
    }

    public void processOneFileInit(String fileName, String fileNameOut) throws FileNotFoundException, IOException {
        // Open the file
        FileInputStream fstream = new FileInputStream(fileName);
        BufferedReader br = new BufferedReader(new InputStreamReader(fstream));

        String strLine;
        //Read File Line By Line
        while ((strLine = br.readLine()) != null) {
            // Print the content on the console
            processOneLineFileInit(strLine, fileNameOut);
        }
        br.close();
    }

    public String processOneLineFileInit(String strLine, String fileNameOut) {
        //System.out.println(strLine);
//        String patern = "<.?(s|S)>";
//        strLine = strLine.replaceAll(patern, ""); //remove </s> and <s> ’
        
        String patern = "”|“|\""; 
        strLine = strLine.replaceAll(patern, " \" "); //remove " or '   -
        
        patern = "’|'";
        strLine = strLine.replaceAll(patern, " ' "); //remove " or '   -
        
        patern = "-|–";
        strLine = strLine.replaceAll(patern, " - ");
        
        patern ="\\.\\.\\.|…";
        strLine = strLine.replaceAll(patern, " ... ");
        
        patern = "<(s|S)>";
        strLine = strLine.replaceAll(patern, Config.SYMBOY_START);
        
        patern = "</(s|S)>";
        strLine = strLine.replaceAll(patern, Config.SYMBOY_END);

//        patern = "(\\s|\\()?\\d+(\\s?-\\s?)\\d+(\\s?-\\s?)\\d+(\\s|\\))?";
//        strLine = strLine.replaceAll(patern, Config.SYMBOL_DATE);  //replace a specific date by date symbol

        patern = "(\\d)+(\\s?)(\\.|,)(\\s?)(\\d)+";
        strLine = strLine.replaceAll(patern, Config.SYMBOL_NUMBER);  //replace a specific number by number symbol

        patern = "\\d+";
        strLine = strLine.replaceAll(patern, Config.SYMBOL_NUMBER); //replace

        //System.out.println(strLine);
        //System.out.println("---------------------------------");

        //separate a sentence to some single phrases
        // at the same time, open file stream in which wrote

        PrintWriter out = null;
        try {
            if (fileNameOut.length()>1) {
                out = new PrintWriter(new BufferedWriter(new FileWriter(fileNameOut, true)));
            }
            strLine= separateSinglePhrase(strLine, out);
            //out.println("the text");
        } catch (IOException e) {
            //System.err.println(e);
            return "";
        } finally {
            if (out != null) {
                out.close();
            }
        }
        return strLine;
    }

    //use for old version (wrong thinking)
//    private void filterBracket(String strLine, PrintWriter out) {
//        //process ()               
//        String pattern = "\\(.[^\\(\\)]+\\)";
//        Pattern r = Pattern.compile(pattern);
//        Matcher m = r.matcher(strLine);
//        String sub;
//        String saveString = strLine;
//        while (m.find()) {
//            sub = saveString.substring(m.start() + 1, m.end() - 1);
//            filterBracket(sub, out);
//            strLine = strLine.replace(saveString.substring(m.start(), m.end()), "");
//        }
//        if (!strLine.contains("(") && !strLine.contains(")")) {
//            separateSinglePhrase(strLine, out, 0);
//        }
//    }

    //separate paragraph to single phrases
    private String separateSinglePhrase(String strLine, PrintWriter out) {

        //process :
        List<String> lsPartern = Arrays.asList(":", ",", ";", "-", "/", "%", "!", "&", "<", ">");
        for (int i=0; i<lsPartern.size(); i++) {
             strLine = strLine.replaceAll(lsPartern.get(i), " "+lsPartern.get(i)+" ");          
        }       

        strLine =strLine.replaceAll("\\.", " . ");
        strLine = strLine.replaceAll("\\?", " ? ");
        strLine = strLine.replaceAll("\\(", " ( ");
        strLine = strLine.replaceAll("\\)", " ) ");
        
        //write these single phrase to output
        if (out != null)
            out.println(strLine);
        
        return strLine;
//        if (!strLine.contains(":") && !strLine.contains(",") && !strLine.contains(";")) {
//            //remove . ? !
//            r = Pattern.compile("\\s-\\s");
//            m = r.matcher(strLine);
//            if (!m.find()) {
//                strLine = strLine.replaceAll("\\.|!|\\?|/|\\.\\.?\\.?|\\*|…", "");
//                //write these single phrase to output
//                out.println(strLine.trim());
//            }
//        }
    }
}
